//
//  SpecialCellTableViewCell.m
//  WisdomZhuHai
//
//  Created by BABLE on 15/4/9.
//  Copyright (c) 2015年 wyd. All rights reserved.
//
//首页新闻列表中的专题cell
#import "TopSpecialCellViewController.h"
#import "DVSwitch.h"

@interface TopSpecialCellViewController()

@property (nonatomic,strong) UIView* headerView;
@property (nonatomic,strong) UIView* headerPopupView;

@property (nonatomic,assign) int currentIndex;
@property (nonatomic,strong) NSMutableArray* newsList;
//承接专题信息列表
//@property (nonatomic,strong) NSMutableArray* imgUrlList;//图片列表
@property (nonatomic,strong) NSArray* newsIdList;//新闻id列表（暂时无用）
@property (nonatomic,strong) NSArray* newsUrlList;//新闻url列表（暂时无用）

@property (nonatomic, strong) NSMutableArray *array_categoryName;
@property (nonatomic, strong) NSMutableArray *array_categoryId;

@property (nonatomic, assign) int category_current;

@property (nonatomic, strong) DVSwitch *topSelectMenu;

@property (nonatomic, assign) int isMenuInited;

@property (nonatomic, assign) int isNotFull;//用于判断专题cell内容是否完整





@end

@implementation TopSpecialCellViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentIndex = -1;
    self.category_current = 0;
    self.isMenuInited = 0;
    self.tableView.backgroundColor = [UIColor whiteColor];
    //    [self setEnableRefresh:YES];
    [self setEnableLoadMore:YES];
    self.pullTableViewDelegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, self.headerView.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height - self.headerView.bounds.size.height);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0, self.tableView.bounds.size.width, 35)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    [self getList:self.category_current];
    
    self.array_categoryName = [[NSMutableArray alloc]init];
    self.array_categoryId = [[NSMutableArray alloc] init];
}

//调取获取资讯（新闻）列表的协议
#pragma mark - Protocol request
- (void)getList:(int)category_id
{
    if(category_id == 0){
        [[ProtocolManager shareInstance] getNewsListWithCategoryId:0 andCount:20 beginNo:_currentIndex + 1 target:self selector:@selector(getListCallback:)];
    }else{
        [[ProtocolManager shareInstance] getNewsListWithCategoryId:[[self.array_categoryId objectAtIndex:category_id] intValue] andCount:20 beginNo:_currentIndex + 1 target:self selector:@selector(getListCallback:)];
    }
    
}
#pragma mark - Protocol call back

- (void)getListCallback:(NSDictionary*)dic
{
    NSLog(@"getListCallback %@", dic);
    if (dic == nil) {
        [self resetPullTableViewState];
        return;
    }
    if (self.currentIndex == -1) {
        self.newsList = [NSMutableArray array];
    }
    if([Common valueForKey:@"category" dic:dic] != nil || [[Common valueForKey:@"category" dic:dic] count] > 0){
        NSArray *array_temp = [NSArray arrayWithArray:[Common valueForKey:@"category" dic:dic]];
        
        if(self.isMenuInited == 0){
            for(NSDictionary *dic_temp in array_temp){
                [self.array_categoryId addObject:[Common valueForKey:@"id" dic:dic_temp]];
                [self.array_categoryName addObject:[Common valueForKey:@"name" dic:dic_temp]];
            }
            self.isMenuInited = 1;
        }
    }
    NSLog(@"%s || array_categoryId:%@, array_categoryName:%@", __FUNCTION__, self.array_categoryId, self.array_categoryName);
    NSArray* newsList = [Common valueForKey:P_LIST dic:dic];
    if ([newsList isKindOfClass:[NSArray class]]) {
        [self.newsList addObjectsFromArray:newsList];
        self.currentIndex += self.newsList.count;
    }
    if (newsList.count < STORE_LIST_RETURN_COUNT) {
        [self setEnableLoadMore:NO];
    }
    else {
        [self setEnableLoadMore:YES];
    }
    [self initMenuWithArray];
    
    [self resetPullTableViewState];
    [self.tableView reloadData];
}


#pragma mark - cell
//cellUI初始化
//参数index为新闻（资讯）列表contorller传入的参数，确定本cell显示哪一条专题信息
//newsList存储的为若干个新闻列表内所有类型的内容，甄别为“专题”类型的工作由新闻（资讯）列表contorller完成
//本方法受SpecialMessageViewController.m调用
- (void)cellSetup:(UITableViewCell*)cell dic:(NSDictionary *)cellDic index:(NSInteger)index;
{
    if (cellDic.count <= 0) {
        return;
    }
    //读取解析后的JSON中的参数
//    NSDictionary *cellDic = _newsList[index];
//    NSString *topicId = [Common valueForKey:P_TOPIC_ID dic:cellDic];
//    NSString *toppicTitle = [Common valueForKey:P_TOPIC_TITLE dic:cellDic];
    NSArray *newsList = [Common valueForKey:P_TOPIC_NEWS dic:cellDic];
    NSDictionary *imgUrlGetter = [[NSDictionary alloc] init];
    NSMutableArray *imgUrlList = [[NSMutableArray alloc] init];//图片列表
    for( int i=0; i<newsList.count; i++){
        imgUrlList[i] = [[newsList objectAtIndex:i] objectForKey:P_TOPIC_NEWS_NPHOTO];
//        NSLog(@"imgUrlList【%d】%@  // 所读信息 %@............................................",i,[imgUrlList objectAtIndex:i],[imgUrlGetter objectForKey:P_TOPIC_NEWS_NPHOTO]);
//        [self.newsUrlList arrayByAddingObject: [Common valueForKey:P_TOPIC_NEWS_URL dic:[newsList objectAtIndex:i]]];
//        [self.newsIdList arrayByAddingObject: [Common valueForKey:P_TOPIC_NEWS_ID dic:[newsList objectAtIndex:i]]];
    }
    
    //初始化UI组件

    //初始化4张图片
    UIImageView *image1 = (UIImageView*)[cell viewWithTag:98001];
    UIImageView *image2 = (UIImageView*)[cell viewWithTag:98002];
    UIImageView *image3 = (UIImageView*)[cell viewWithTag:98003];
    UIImageView *image4 = (UIImageView*)[cell viewWithTag:98004];
    NSArray *imageList = [[NSArray alloc] initWithObjects:image1,image2,image3,image4, nil];
    
    for(int i=0;i<4;i++ ){
        if(imgUrlList.count-1 < i){
            for(int j=i;j<4;j++) [imageList[j] setImage:[UIImage imageNamed:@"placeholder@2x"]];//mark
            self.isNotFull = 1;
        }else{
            [imageList[i] setImageWithURL:[Common getFullDownloadPathByRelativePath:[imgUrlList objectAtIndex:i]]];
        }
    }
    //初始化title
    UILabel *titleLab = (UILabel*)[cell viewWithTag:100001];
    titleLab.text = [Common valueForKey:P_TOPIC_TITLE dic:cellDic];
    if(titleLab.text == nil){
        titleLab.text = @"专题";
        self.isNotFull = 1;
    }
    [titleLab setNumberOfLines:1];
    [titleLab sizeToFit];
    
}

- (void)initMenuWithArray{
    if(self.topSelectMenu == nil){
        self.topSelectMenu = [DVSwitch switchWithStringsArray:self.array_categoryName];
        self.topSelectMenu.frame = CGRectMake(0, 0, 320, 35);
        self.topSelectMenu.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];
        self.topSelectMenu.sliderColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];
        self.topSelectMenu.labelTextColorInsideSlider = [UIColor colorWithRed:20/255.0 green:180/255.0 blue:220/255.0 alpha:1];
        self.topSelectMenu.labelTextColorOutsideSlider = [UIColor blackColor];
        self.topSelectMenu.cornerRadius = 5;
        [self.topSelectMenu addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.topSelectMenu];
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row >= self.newsList.count) {
        return;
    }
    NSDictionary* dic = self.newsList[indexPath.row];
    NSString *url = [Common valueForKey:P_URL dic:dic];
    WebIOS *web = [[WebIOS alloc]initWithFrame:[Common shareInstance].contentFrame url:[Common getUrlByString:url]];
    web.target_id = [[Common valueForKey:@"id" dic:dic] intValue];
    web.type = @"NEWS";
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}

#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    [self setPullTableIsLoadMore:YES];
    self.currentIndex = -1;
    [self getList:self.category_current];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    [self getList:self.category_current];
}

- (void)resetPullTableViewState
{
    [self setPullTableIsRefreshing:NO];
    [self setPullTableIsLoadMore:NO];
}

@end
